package com.ycu.jk16.mall.dao.provider;

import static com.ycu.jk16.mall.util.common.AndroidMalConstant.SHIPPINGTABLE;

import java.util.Map;

import org.apache.ibatis.jdbc.SQL;

import com.ycu.jk16.mall.domain.Shipping;
import com.ycu.jk16.mall.util.common.PageUtil;

public class ShippingDynaSqlProvider {
	// 动态插入地址
	public String insertShipping(Shipping shipping) {
		return new SQL() {
			{
				INSERT_INTO(SHIPPINGTABLE);
				// VALUES("数据库字段", "#{java属性名}");
				if (shipping.getUser().getId() != null) {
					VALUES("user_id", shipping.getUser().getId().toString());
				}
				if (shipping.getName() != null && !shipping.getName().equals("")) {
					VALUES("name", "#{name}");
				}
				if (shipping.getProvince() != null && !shipping.getProvince().equals("")) {
					VALUES("province", "#{province}");
				}
				if (shipping.getCity() != null && !shipping.getCity().equals("")) {
					VALUES("city", "#{city}");
				}
				if (shipping.getAddress() != null && !shipping.getAddress().equals("")) {
					VALUES("address", "#{address}");
				}
				if (shipping.getZip() != null && !shipping.getZip().equals("")) {
					VALUES("zip", "#{zip}");
				}
				if (shipping.getPhone() != null && !shipping.getPhone().equals("")) {
					VALUES("phone", "#{phone}");
				}
				if (shipping.getMobile() != null && !shipping.getMobile().equals("")) {
					VALUES("mobile", "#{mobile}");
				}
			}
		}.toString();
	}

	// 动态修改地址
	public String updateShipping(Shipping shipping) {
		return new SQL() {
			{
				UPDATE(SHIPPINGTABLE);
				if (shipping.getId() != null) {
					// SET("数据库字段名", "Java属性名")
					if (shipping.getName() != null && !shipping.getName().equals("")) {
						SET(" name = #{name} ");
					}
					if (shipping.getProvince() != null && !shipping.getProvince().equals("")) {
						SET(" province = #{province} ");
					}
					if (shipping.getCity() != null && !shipping.getCity().equals("")) {
						SET(" city = #{city} ");
					}
					if (shipping.getAddress() != null && !shipping.getAddress().equals("")) {
						SET(" address = #{address} ");
					}
					if (shipping.getZip() != null && !shipping.getZip().equals("")) {
						SET(" zip = #{zip} ");
					}
					if (shipping.getPhone() != null && !shipping.getPhone().equals("")) {
						SET(" phone = #{phone} ");
					}
					if (shipping.getMobile() != null && !shipping.getMobile().equals("")) {
						SET(" mobile = #{mobile} ");
					}
				}

				WHERE(" id = #{id} ");
			}
		}.toString();
	}

	// 返回地址列表
	public String selectWithParam(Map<String, Object> params) {
		String sql = new SQL() {
			{
				SELECT("*");
				FROM(SHIPPINGTABLE);
				Integer userId = (Integer) params.get("userId");
				if (userId != null) {
					WHERE(" user_id = #{userId} ");
				}
			}
		}.toString();

		// 此处加入了分页限制，每页显示 = (页数-1)*每页显示数, 每页显示数
		if (params.get("page") != null) {
			PageUtil page = (PageUtil) params.get("page");
			int pageIndex = page.getPageIndex();
			int pageSize = page.getPageSize();

			sql += " limit " + (pageIndex - 1) * pageSize + "," + pageSize;
		}

		return sql;
	}
}
